/**
 * <p>Title: ServiceZMCEC3.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-9 下午02:18:33  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zm.ce.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.ce.domain.ZMCEAccessories;
import com.baosight.sczx.zm.ce.domain.ZMCEMaintenanceStandard;

/**
 * 备品配件
 * <p>Title: ServiceZMCEC3.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-9 下午02:18:33  张志芳  Create
 *</pre>
 */
public class ServiceZMCEC3 extends ServiceEPBase {
	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEAccessories</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-8上午09:57:14 张志芳
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);		
		return outInfo;
	}

	/**
	 * 设备查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEAccessories</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:23:36 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdEquipmentId = inInfo.getString("fdEquipmentId");
		StaticTools.setQueryParameters(inInfo, "fdEquipmentId", fdEquipmentId, true);//添加查询条件		
		EiInfo outInfo = super.query(inInfo, "ZMCEAccessories.query", new ZMCEAccessories());

		return outInfo;
	}
	
	/**
	 * 设备新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEAccessories</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:23:46 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验名称、简称是否重复
		boolean validateValue = validate(inInfo, "ZMCEAccessories.validate", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_MAINTENANCESTANDARD"));//主键
			EiInfo outinfo = super.insert(inInfo, "ZMCEAccessories.insert", "result");
			return outinfo;
		}
	}
	
	/**
	 * 设备修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEAccessories</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:23:56 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验名称、简称是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean validateValue = validate(inInfo, "ZMCEAccessories.validate", params);
		if(validateValue) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZMCEAccessories.update");
			return outInfo;
		}
	}

	/**
	 * 设备删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEAccessories</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:24:03 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCEAccessories.delete");
		EiInfo outInfo = query(inInfo);
	    outInfo.setMsg(_temp.getMsg());
	    outInfo.setDetailMsg(_temp.getDetailMsg());
		
		return outInfo;
	}

	

	
	/**
	 * 校验重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:26:16 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdCode=inInfo.getBlock("result").getCellStr(0, "fdCode");
		String fdName=inInfo.getBlock("result").getCellStr(0, "fdName");
		params.put("fdCode", fdCode);
		
		params.put("fdName", fdName);
		params.put("fdSpecifications", inInfo.getBlock("result").getCellStr(0, "fdSpecifications"));
		params.put("fdModel", inInfo.getBlock("result").getCellStr(0, "fdModel"));
		params.put("fdEquipmentId", inInfo.getBlock("result").getCellStr(0, "fdEquipmentId"));
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			if(result.size() > 1)
			{
				inInfo.setMsg("编号不能重复！该设备下的备品配件名称、型号和规格必须联合唯一！");
				return true;
			}			
			ZMCEAccessories map = (ZMCEAccessories)result.get(0);
			if(map.getFdCode().toString().equals(fdCode) && map.getFdName().toString().equals(fdName))
			{
				inInfo.setMsg("编号不能重复！该设备下的备品配件名称、型号和规格必须联合唯一！");
				return true;
			}
			if(map.getFdCode().toString().equals(fdCode) )
			{
				inInfo.setMsg("编号不能重复！");
				return true;
			}
			if(map.getFdName().toString().equals(fdName))
			{
				inInfo.setMsg("该设备下的备品配件名称、型号和规格必须联合唯一！");
				return true;
			}
		}
		return false;
	}
	
}

